package com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.repository.Impl;


import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.api.local.org.UserAPI;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.division.repository.DivisionRepository;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.org.role.repository.entity.RoleEntity;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.commen.util.ServerUtil;
import com.css.zfzx.fwsjcj.modules.group.entity.FwGroupEntity;
import com.css.zfzx.fwsjcj.modules.group.entity.FwGroupUserEntity;
import com.css.zfzx.fwsjcj.modules.group.service.FwGroupService;
import com.css.zfzx.fwsjcj.modules.group.service.FwGroupUserService;
import com.css.zfzx.fwsjcj.modules.quality.vo.QualityVo;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwFormEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.repository.TaskFormQueryRepository;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.vo.TaskFormPCQueryVo;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.vo.TaskFormQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@PropertySource("classpath:platform-config.yml")
public class TaskFormQueryRepositoryImpl implements TaskFormQueryRepository {
    @Autowired
    EntityManager entityManager;
    @Value("${role.superCode}")
    private String superCode;
    @Value("${role.provinceCode}")
    private String provinceCode;
    @Value("${role.cityCode}")
    private String cityCode;
    @Value("${role.dataCollectCode}")
    private String dataCollectCode;
    @Autowired
    private DivisionRepository divisionRepository;
    @Autowired
    private FwGroupUserService fwGroupUserService;
    @Autowired
    private FwGroupService fwGroupService;

    @Override
    public Page<FwFormEntity> findPage(HttpServletRequest request, TaskFormQueryVO taskFormQueryVO, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder(" select * from fw_form");
        StringBuilder whereSql = new StringBuilder(" where is_valid = " + Constant.IS_VALID_YES);
        if (!StringUtils.isEmpty(taskFormQueryVO.getType()) && !"4".equals(taskFormQueryVO.getType())) {
            whereSql.append(" and sp_type =:spType");
        }
        if (!StringUtils.isEmpty(taskFormQueryVO.getBuildType()) && !"4".equals(taskFormQueryVO.getBuildType())) {
            whereSql.append(" and buliding_type =:buildType");
        }
        whereSql.append(" and task_id =:taskId");
        whereSql.append(" and creat_user_Id = '" + PlatformSessionUtils.getUserId() + "'");

        StringBuilder orderBy = new StringBuilder(" order by creat_time desc");
        Query nativeQuery = entityManager.createNativeQuery(sql.append(whereSql).append(orderBy).toString(), FwFormEntity.class);
        StringBuilder countSql = new StringBuilder(" select count(*) from fw_form");
        Query countQuery = entityManager.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(taskFormQueryVO.getType()) && !"4".equals(taskFormQueryVO.getType())) {
            nativeQuery.setParameter("spType", taskFormQueryVO.getType());
            countQuery.setParameter("spType", taskFormQueryVO.getType());
        }
        if (!StringUtils.isEmpty(taskFormQueryVO.getBuildType()) && !"4".equals(taskFormQueryVO.getBuildType())) {
            nativeQuery.setParameter("buildType", taskFormQueryVO.getBuildType());
            countQuery.setParameter("buildType", taskFormQueryVO.getBuildType());
        }
        nativeQuery.setParameter("taskId", taskFormQueryVO.getTaskId());
        countQuery.setParameter("taskId", taskFormQueryVO.getTaskId());
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        nativeQuery.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        nativeQuery.setMaxResults(pageable.getPageSize());
        List<FwFormEntity> resultList = nativeQuery.getResultList();
        BigInteger countNum = (BigInteger) countQuery.getSingleResult();
        Page<FwFormEntity> page = new PageImpl<>(resultList, pageable, countNum.longValue());
        return page;
    }

    @Override
    public Page<FwFormEntity> findAllPage(String role,TaskFormPCQueryVo taskFormPCQueryVo, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder(" select * from fw_form");
        StringBuilder whereSql = new StringBuilder(" where is_valid = " + Constant.IS_VALID_YES);
        // 数据审批状态：未审核 审核通过 审核未通过
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getStatus())) {
            whereSql.append(" and sp_type =:spType");
        }
        // 建筑类型
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingType())) {
            whereSql.append(" and buliding_type =:buildType");
        }
        // 任务名称
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getTaskName())) {
            whereSql.append(" and task_name like :taskName");
        }
        // 采集人
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getCollecPerson())) {
            whereSql.append(" and collec_person like :collecPerson");
        }
        // 建筑编码
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingCode())) {
            whereSql.append(" and buliding_code like :buildingCode");
        }
        // 建筑物名称
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingName())) {
            whereSql.append(" and building_name like :buildingName");
        }

        whereSql.append(" and sp_type !=" + Constant.NO_SUB);

        String userId = PlatformSessionUtils.getUserId();
        /*List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        //是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        //是否为省级管理员
        boolean containtRolePro = ServerUtil.isContaintRole(roles, provinceCode);
        //是否为市级管理员
        boolean containtRoleCity = ServerUtil.isContaintRole(roles, cityCode);
        //是否为信息审核员
        boolean dataCollect = ServerUtil.isContaintRole(roles, dataCollectCode);*/

        if ("1".equals(role)){
            // 所属省份
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoProvince())) {
                whereSql.append(" and belongto_province like :belongtoProvince");
            }
            // 所属城市
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoCity())) {
                whereSql.append(" and belongto_city like :belongtoCity");
            }
            // 所属区县
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())) {
                whereSql.append(" and belongto_area like :belongtoArea");
            }
        }
        if ("2".equals(role)){
            whereSql.append(" and belongto_province like :belongtoProvince");
            // 所属城市
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoCity())) {
                whereSql.append(" and belongto_city like :belongtoCity");
            }
            // 所属区县
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())) {
                whereSql.append(" and belongto_area like :belongtoArea");
            }
        }
        if ("3".equals(role)){
            whereSql.append(" and belongto_province like :belongtoProvince");
            whereSql.append(" and belongto_city like :belongtoCity");
            // 所属区县
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())) {
                whereSql.append(" and belongto_area like :belongtoArea");
            }
        }
        if ("4".equals(role)){
            whereSql.append(" and belongto_province like :belongtoProvince");
            whereSql.append(" and belongto_city like :belongtoCity");
            whereSql.append(" and belongto_area like :belongtoArea");
        }
        if ("6".equals(role)){
            whereSql.append(" and creat_user_Id in :listUser");
        }
        if (PlatformObjectUtils.isEmpty(role)){
            whereSql.append(" and creat_user_Id =:userId");
        }

        StringBuilder orderBy = new StringBuilder(" order by creat_time desc");
        Query nativeQuery = entityManager.createNativeQuery(sql.append(whereSql).append(orderBy).toString(), FwFormEntity.class);
        StringBuilder countSql = new StringBuilder(" select count(*) from fw_form");
        Query countQuery = entityManager.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getStatus())) {
            nativeQuery.setParameter("spType", taskFormPCQueryVo.getStatus());
            countQuery.setParameter("spType", taskFormPCQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingType())) {
            nativeQuery.setParameter("buildType", taskFormPCQueryVo.getBuildingType());
            countQuery.setParameter("buildType", taskFormPCQueryVo.getBuildingType());
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getTaskName())) {
            nativeQuery.setParameter("taskName", "%" + taskFormPCQueryVo.getTaskName() + "%");
            countQuery.setParameter("taskName", "%" + taskFormPCQueryVo.getTaskName() + "%");
        }
        // 采集人
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getCollecPerson())) {
            nativeQuery.setParameter("collecPerson", "%" + taskFormPCQueryVo.getCollecPerson() + "%");
            countQuery.setParameter("collecPerson", "%" + taskFormPCQueryVo.getCollecPerson() + "%");
        }
        // 建筑编码
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingCode())) {
            nativeQuery.setParameter("buildingCode", "%" + taskFormPCQueryVo.getBuildingCode() + "%");
            countQuery.setParameter("buildingCode", "%" + taskFormPCQueryVo.getBuildingCode() + "%");
        }
        // 建筑物名称
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingName())) {
            nativeQuery.setParameter("buildingName", "%" + taskFormPCQueryVo.getBuildingName() + "%");
            countQuery.setParameter("buildingName", "%" + taskFormPCQueryVo.getBuildingName() + "%");
        }


        //省市县条件 赋值
        UserAPI userAPI = PlatformAPI.getOrgAPI().getUserAPI();
        DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
        String divisionId = mainDept.getDivisionId();
        DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);

        if ("1".equals(role)){
            // 所属省份
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoProvince())) {
                nativeQuery.setParameter("belongtoProvince", "%" + taskFormPCQueryVo.getBelongtoProvince() + "%");
                countQuery.setParameter("belongtoProvince", "%" + taskFormPCQueryVo.getBelongtoProvince() + "%");
            }
            // 所属城市
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoCity())) {
                nativeQuery.setParameter("belongtoCity", "%" + taskFormPCQueryVo.getBelongtoCity() + "%" );
                countQuery.setParameter("belongtoCity", "%" + taskFormPCQueryVo.getBelongtoCity()  + "%");
            }
            // 所属市区
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())) {
                nativeQuery.setParameter("belongtoArea", "%" + taskFormPCQueryVo.getBelongtoArea()+ "%");
                countQuery.setParameter("belongtoArea", "%" + taskFormPCQueryVo.getBelongtoArea()+ "%");
            }
        }
        if ("2".equals(role)){
            List<String> list = Arrays.asList(validDivisionById.getAllParentId().split(","));
            divisionId = list.get(1);
            DivisionEntity divisionEntity = divisionRepository.findValidDivisionById(divisionId);
            String divisionName = divisionEntity.getDivisionName();
            nativeQuery.setParameter("belongtoProvince", "%" + divisionName + "%");
            countQuery.setParameter("belongtoProvince", "%" + divisionName + "%");

            // 所属城市
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoCity())) {
                nativeQuery.setParameter("belongtoCity", "%" + taskFormPCQueryVo.getBelongtoCity() + "%" );
                countQuery.setParameter("belongtoCity", "%" + taskFormPCQueryVo.getBelongtoCity()  + "%");
            }
            // 所属市区
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())) {
                nativeQuery.setParameter("belongtoArea", "%" + taskFormPCQueryVo.getBelongtoArea()+ "%");
                countQuery.setParameter("belongtoArea", "%" + taskFormPCQueryVo.getBelongtoArea()+ "%");
            }
        }
        if ("3".equals(role)){
            List<String> list = Arrays.asList(validDivisionById.getAllParentId().split(","));
            divisionId = list.get(1);
            DivisionEntity divisionEntity = divisionRepository.findValidDivisionById(divisionId);
            String divisionName = divisionEntity.getDivisionName();
            nativeQuery.setParameter("belongtoProvince", "%" + divisionName + "%");
            countQuery.setParameter("belongtoProvince", "%" + divisionName + "%");
            divisionId = list.get(2);
            divisionEntity = divisionRepository.findValidDivisionById(divisionId);
            divisionName = divisionEntity.getDivisionName();
            nativeQuery.setParameter("belongtoCity",  "%" + divisionName + "%");
            countQuery.setParameter("belongtoCity",  "%" + divisionName + "%");
            // 所属市区
            if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())) {
                nativeQuery.setParameter("belongtoArea", "%" + taskFormPCQueryVo.getBelongtoArea()+ "%");
                countQuery.setParameter("belongtoArea", "%" + taskFormPCQueryVo.getBelongtoArea()+ "%");
            }
        }
        if ("4".equals(role)){
            List<String> list = Arrays.asList(validDivisionById.getAllParentId().split(","));
            divisionId = list.get(1);
            DivisionEntity divisionEntity = divisionRepository.findValidDivisionById(divisionId);
            String divisionName = divisionEntity.getDivisionName();
            nativeQuery.setParameter("belongtoProvince", "%" + divisionName + "%");
            countQuery.setParameter("belongtoProvince", "%" + divisionName + "%");
            divisionId = list.get(2);
            divisionEntity = divisionRepository.findValidDivisionById(divisionId);
            divisionName = divisionEntity.getDivisionName();
            nativeQuery.setParameter("belongtoCity",  "%" + divisionName + "%");
            countQuery.setParameter("belongtoCity",  "%" + divisionName + "%");
            divisionId = list.get(3);
            divisionEntity = divisionRepository.findValidDivisionById(divisionId);
            divisionName = divisionEntity.getDivisionName();
            nativeQuery.setParameter("belongtoArea", "%"+divisionName+"%");
            countQuery.setParameter("belongtoArea", "%"+divisionName+"%");
        }
        if ("6".equals(role)){
            //组长
            //查看组长的小组
            List<FwGroupEntity> byGroupLeaderId = fwGroupService.findByGroupLeaderId(userId);
            List<String> listGroup = new ArrayList<>();
            byGroupLeaderId.forEach(fwGroupEntity -> {
                listGroup.add(fwGroupEntity.getId());
            });
            List<List<FwGroupUserEntity>> lists = new ArrayList<>();
            listGroup.forEach(s -> {
                lists.add(fwGroupUserService.findByGroupId(s));
            });
            List<String> listUser = new ArrayList<>();
            for (List<FwGroupUserEntity> listU : lists) {
                for (FwGroupUserEntity fw : listU) {
                    listUser.add(fw.getUserId());
                }
            }
            listUser.add(userId);
            //利用jdk8特性对list进行去重
            List<String> listUserId = listUser.stream().distinct().collect(Collectors.toList());
            nativeQuery.setParameter("listUser", listUserId);
            countQuery.setParameter("listUser", listUserId);
        }
        if (PlatformObjectUtils.isEmpty(role)){
            whereSql.append(" and create_user =:userId");
            nativeQuery.setParameter("userId", userId);
            countQuery.setParameter("userId", userId);
        }

        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        nativeQuery.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        nativeQuery.setMaxResults(pageable.getPageSize());
        List<FwFormEntity> resultList = nativeQuery.getResultList();
        BigInteger countNum = (BigInteger) countQuery.getSingleResult();
        Page<FwFormEntity> page = new PageImpl<>(resultList, pageable, countNum.longValue());
        return page;
    }

    @Override
    public Page<FwFormEntity> findPage(QualityVo qualityVo, int curPage, int pageSize) {
        // 1.主sql
        StringBuilder sql = new StringBuilder(" select * from fw_form");
        // 2.条件sql
        StringBuilder whereSql = new StringBuilder(" where is_valid = " + Constant.IS_VALID_YES);
        // 关联查询任务ID
        whereSql.append(" and task_id =:taskId");
        // 条件查询质检状态
        if (!StringUtils.isEmpty(qualityVo.getQualityStatus())) {
            whereSql.append(" and quality_status =:qualityStatus");
        } else {
            // 查询不为0（0未完成审核不能质检）
            whereSql.append(" and quality_status != 0");
        }
        // 3.排序
        StringBuilder orderBy = new StringBuilder(" order by creat_time desc");

        Query nativeQuery = entityManager.createNativeQuery(sql.append(whereSql).append(orderBy).toString(), FwFormEntity.class);
        StringBuilder countSql = new StringBuilder(" select count(*) from fw_form");
        Query countQuery = entityManager.createNativeQuery(countSql.append(whereSql).toString());

        // 模糊查询参数赋值
        if (!StringUtils.isEmpty(qualityVo.getQualityStatus())) {
            nativeQuery.setParameter("qualityStatus", qualityVo.getQualityStatus());
            countQuery.setParameter("qualityStatus", qualityVo.getQualityStatus());
        }
        nativeQuery.setParameter("taskId", qualityVo.getTaskId());
        countQuery.setParameter("taskId", qualityVo.getTaskId());

        // 返回分页数据
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        nativeQuery.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        nativeQuery.setMaxResults(pageable.getPageSize());
        List<FwFormEntity> resultList = nativeQuery.getResultList();
        BigInteger countNum = (BigInteger) countQuery.getSingleResult();
        Page<FwFormEntity> page = new PageImpl<>(resultList, pageable, countNum.longValue());
        return page;
    }

    @Override
    public List<String> findFormIdListBybuildingType(TaskFormPCQueryVo taskFormPCQueryVo) {
        StringBuilder sql = new StringBuilder(" select form_id from fw_form");
        StringBuilder whereSql = new StringBuilder(" where is_valid = " + Constant.IS_VALID_YES);
        // 所属任务
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getTaskName())){
            whereSql.append(" and task_name like :taskName");
        }
        // 数据状态
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getStatus())){
            whereSql.append(" and sp_type =:spType");
        }
        // 建筑物类别
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingType())){
            whereSql.append(" and buliding_type =:bulidingType");
        }
        // 省
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoProvince())){
            whereSql.append(" and belongto_province =:belongtoProvince");
        }
        // 市
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoCity())){
            whereSql.append(" and belongto_city =:belongtoCity");
        }
        // 区
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())){
            whereSql.append(" and belongto_area =:belongtoArea");
        }
        // 建筑编号
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingCode())){
            whereSql.append(" and buliding_code like :buildingCode");
        }
        // 建筑物名称
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingName())){
            whereSql.append(" and building_name like :buildingName");
        }
        // 采集人
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getCollecPerson())){
            whereSql.append(" and collec_person like :collecPerson");
        }
        whereSql.append(" and sp_type !=" + Constant.NO_SUB);
        String userId = PlatformSessionUtils.getUserId();
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        //是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        //是否为省级管理员
        boolean containtRolePro = ServerUtil.isContaintRole(roles, provinceCode);
        //是否为市级管理员
        boolean containtRoleCity = ServerUtil.isContaintRole(roles, cityCode);
        //如果不是只能查看个人表单
        if (!containtRole) {
            if (!containtRolePro && !containtRoleCity) {
                whereSql.append(" and creat_user_Id =:userId");
            }
            if (containtRolePro || containtRoleCity) {
                whereSql.append(" and building_address like :divisionName");
            }
        }
        StringBuilder orderBy = new StringBuilder(" order by creat_time desc");
        Query nativeQuery = entityManager.createNativeQuery(sql.append(whereSql).append(orderBy).toString());
        // 赋值
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getTaskName())) {
            // 所属任务
            nativeQuery.setParameter("taskName","%" + taskFormPCQueryVo.getTaskName()+ "%");
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getStatus())) {
            // 数据状态
            nativeQuery.setParameter("spType", taskFormPCQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingType())) {
            // 建筑物类别
            nativeQuery.setParameter("bulidingType", taskFormPCQueryVo.getBuildingType());
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoProvince())) {
            // 省
            nativeQuery.setParameter("belongtoProvince", taskFormPCQueryVo.getBelongtoProvince());
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoCity())) {
            // 市
            nativeQuery.setParameter("belongtoCity", taskFormPCQueryVo.getBelongtoCity());
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBelongtoArea())) {
            // 区
            nativeQuery.setParameter("belongtoArea", taskFormPCQueryVo.getBelongtoArea());
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingCode())) {
            // 建筑编号
            nativeQuery.setParameter("buildingCode","%" + taskFormPCQueryVo.getBuildingCode()+ "%");
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getBuildingName())) {
            // 建筑物名称
            nativeQuery.setParameter("buildingName","%" + taskFormPCQueryVo.getBuildingName()+ "%");
        }
        if (!StringUtils.isEmpty(taskFormPCQueryVo.getCollecPerson())) {
            // 采集人
            nativeQuery.setParameter("collecPerson","%" + taskFormPCQueryVo.getCollecPerson()+ "%");
        }
        if (!containtRole) {
            // 用户信息
            if (!containtRolePro && !containtRoleCity) {
                nativeQuery.setParameter("userId", PlatformSessionUtils.getUserId());
            }
            if (containtRolePro || containtRoleCity) {
                DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
                String divisionId = mainDept.getDivisionId();
                DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
                String divisionName = validDivisionById.getDivisionName();
                nativeQuery.setParameter("divisionName", "%" + divisionName + "%");
            }
        }
        return nativeQuery.getResultList();
    }

}
